﻿
var currentStep;
var startStep = 0;
var alreadyRated = 0;
$(document).ready(function () {
    currentStep = startStep;
    initContentObject();
    if (typeof setdefcontent == "function") {
        setdefcontent();
    }
    $("#add-online-content").dialog({
        autoOpen: false,
        height: 500,
        width: 600,
        modal: true,
        create: function (event, ui) {
            loadStep(currentStep);
        },
        close: function () {
            if (currentStep == 6) {
                window.location.reload();
            } else {
                currentStep = startStep;
                initContentObject();
                loadStep(currentStep);
            }
        }
    });
    $("#filter-online-content").dialog({
        autoOpen: false,
        height: 500,
        width: 600,
        modal: true,
        buttons: [
			{
			    text: window.Resources.ApplyButton,
			    click: function () {
			        var categories = "";
			        var languages = "";
			        var priceFilter = $("#priceFilterList").val();
			        var videoTypeFilter = $("#videoTypeFilterList").val();
			        var selectedCountry = $("#SelectedCountry").val();
			        $("input[name='Category']").each(function (index) {
			            if ($(this)[0].checked) {
			                categories += $(this).data("cat") + ";";
			            }
			        });

			        $("#filter-online-content .lang a.active").each(function (index) {
			            languages += $(this)[0].id + ";";
			        });
			       // alert($("#actionmethod").val());
			        window.location.href = $("#actionmethod").val() + "&categories=" + categories + "&languages=" + languages + "&priceFilter=" + priceFilter + "&videoType=" + videoTypeFilter + "&selectedCountry=" + selectedCountry;
			    }
			}
        ]
    });
    $("#filter-online-content .lang a").click(function () {
        if ($(this).hasClass("active")) {
            $(this).removeClass("active");
        } else {
            $(this).addClass("active");
        }
    });
    $("#searchButton").click(function () {
        window.location.href = $("#actionmethod").val() + "&search=" + $("#search").val();
    });
    
    $("#add-new-comment").dialog({
        autoOpen: false,
        height: 250,
        width: 300,
        modal: true,
        buttons: [
			{
			    text: "Ok",
			    click: function () {
			        $(this).dialog("close");
			        var positive = $("#IsLike").val();
			        var onlineContentId = $("#Id").val();
			        $.ajax({
			            type: "POST",
			            url: window.MAIN_WEB_PATH + "Ajax/AddCommentsMessage",
			            dataType: "json",
			            data: { id: onlineContentId, isLike: positive, text: $("#NewMessage").val() },
			            success: function (data, textStatus, jqXHR) {
			                alreadyRated = 1;
			                if (data != "") {
			                    if (positive=='true') {
			                        $("#content-" + onlineContentId + " .likeValue").text(data);
			                    }
			                    else {
			                        $("#content-" + onlineContentId + " .unlikeValue").text(data);
			                    }
			                }
			            },
			            error: function (data, textStatus, jqXHR) {}
			        });
			    }
			}
        ]
    });

    $("#content-pay-button").click(function () {
        $.ajax({
            type: "POST",
            url: window.MAIN_WEB_PATH + "Ajax/PayForContent",
            dataType: "json",
            data: { onlineContentId: $(this).data("content-id") },
            success: function (data, textStatus, jqXHR) {
                if (data == "money") {
                    $("#content-payment-link").show();
                    $("#content-payment-result").text(window.Resources.CommonPaymentAdditional);
                } else if (data == "paid") {
                    $("#content-pay-button").hide();
                    $("#content-payment-link").hide();
                    $("#content-payment-result").text(window.Resources.CommonPaid);
                    window.location.reload();
                } else {
                    $("#content-payment-result").text("error, try again");
                }
            }
        });
    });
});

function initContentObject() {
   // alert("init");
    window.OnlineContent = new Object();
    //
    window.OnlineContent.QualityString = "";
    window.OnlineContent.ViewrCnt = 20;
    window.OnlineContent.VideoContentType = "Free";
    window.OnlineContent.MaxTime = 1;
    //
    window.OnlineContent.QualityStringCost = 0;
    window.OnlineContent.ViewrCntCost = 0;
    window.OnlineContent.MaxTimeMult = 0;
    window.OnlineContent.ContentTypeMult = 0;
    //
    window.OnlineContent.Name = "";
    window.OnlineContent.Description = "";
    window.OnlineContent.Categories = "";
    window.OnlineContent.ThumbnailFileName = "";
    window.OnlineContent.ThumbnailFileUrl = "";
    window.OnlineContent.Tags = "";
    window.OnlineContent.CamParam = "";
    window.OnlineContent.BroadcastType = "";
    window.OnlineContent.BroadcastDescription = "";
    window.OnlineContent.ManualStartTime = "";
    window.OnlineContent.StartTime = "";
    window.OnlineContent.EndTime = "";
    window.OnlineContent.ManualStartDate = "";
    window.OnlineContent.StartDate = "";
    window.OnlineContent.EndDate = "";
    window.OnlineContent.SelectedCulture = "en-US";
    window.OnlineContent.SelectedCountry = "";
    window.OnlineContent.Price = "";
    window.OnlineContent.Password = "";
    window.OnlineContent.TeamCount = "";
    window.OnlineContent.TeamDocumentsUrlsPaid = "";
    window.OnlineContent.TeamDocumentsUrlsPaidGroups = "";
    window.OnlineContent.TeamDocumentsNamesPaid = "";
    window.OnlineContent.TeamDocumentsNamesPaidGroups = "";
    window.OnlineContent.TeamDocumentsListHtmlPaid = "";
    window.OnlineContent.TeamDocumentsListHtmlPaidGroups = "";
    window.OnlineContent.IsMeetingPaid = false;
    window.OnlineContent.IsDraft = false;
    window.OnlineContent.AutoHistory = false;
    window.OnlineContent.CreatedContentId = -1;
    window.OnlineContent.RemoveAds = false;
}

function loadStep(step) {
    $("#wizard").load(window.MAIN_WEB_PATH + "Ajax/WizardOnlineStep?step=" + step, function () {
        if (typeof loadStepData == "function") {
            loadStepData();
        }
    });

    if (step == startStep) {
        $("#add-online-content").dialog("option", "buttons", [
			{
			    text: window.Resources.NextButton,
			    click: function () {
			        if (isStepValid()) {
			            if (typeof saveStepData == "function") {
			                saveStepData();
			            }
			            currentStep++;
			            loadStep(currentStep);
			        }
			    }
			}
        ]);
    } else if (step == 6) {
        $("#add-online-content").dialog("option", "buttons", [
			{
			    text: window.Resources.CloseButton,
			    click: function () {
			        $(this).dialog("close");
			    }
			}
        ]);
    } else if (step == 7) {
        $("#add-online-content").dialog("option", "buttons", [
			{
			    text: window.Resources.TryAgainButton,
			    click: function () {
			        currentStep = startStep;
			        initContentObject();
			        loadStep(currentStep);
			    }
			},
			{
			    text: window.Resources.CloseButton,
			    click: function () {
			        $(this).dialog("close");
			    }
			}
        ]);
    } else {
        $("#add-online-content").dialog("option", "buttons", [
			{
			    text: window.Resources.PreviousButton,
			    click: function () {
			        if (typeof saveStepData == "function") {
			            saveStepData();
			        }

			        if (currentStep == 5)
			        { currentStep = currentStep - 2 }/*Hapaniuk change at 17.08.2013*/
			        else
			        { currentStep--; }

			        loadStep(currentStep);
			    }
			},
			{
			    text: window.Resources.NextButton,
			    click: function () {
			        if (isStepValid()) {
			            if (typeof saveStepData == "function") {
			                saveStepData();
			            }
			            if (currentStep == 3)
			            { currentStep = currentStep + 2 }/*Hapaniuk change at 17.08.2013*/
			            else
			            {currentStep++;}

			            if (currentStep == 6) {
			                window.OnlineContent.TeamDocumentsListHtmlPaid = "";
			                window.OnlineContent.TeamDocumentsListHtmlPaidGroups = "";
			                $.ajax({
			                    type: "POST",
			                    url: window.MAIN_WEB_PATH + "Ajax/SaveOnlineContent",
			                    dataType: "json",
			                    data: { content: JSON.stringify(window.OnlineContent) },
			                    success: function (data, textStatus, jqXHR) {
			                        if (data < 0) {
			                            currentStep = 7;
			                            loadStep(currentStep);
			                            return;
			                        }
			                    }
			                });
			            }

			            loadStep(currentStep);
			        }
			    }
			}
        ]);
    }
}

function isStepValid() {
    var isValid = $("#wizard form").valid();
    if (isValid && currentStep== startStep &&  typeof ifAllOk == "function") {
        isValid = ifAllOk();
    }
 
    //if (isValid && typeof validateCaptcha == "function" && currentStep == 5) {
    //    isValid = validateCaptcha();
    //} /*HPN change at 12.09.13*/
    
    return isValid;
}

function rateOnlineContent(onlineContentId, positive, sender) {

    $.ajax({
        type: "POST",
        url: window.MAIN_WEB_PATH + "Ajax/RateOnlineContent",
        dataType: "json",
        data: { onlineContentId: onlineContentId, positive: positive },
        success: function (data, textStatus, jqXHR) {
            if (data != "") {
                if (positive == "true") {
                    $("#content-" + onlineContentId + " .likeValue").text(data);
                } else {
                    $("#content-" + onlineContentId + " .unlikeValue").text(data);
                }
                $(sender).parent().find("a").removeAttr("onclick");
            }
        }
    });
}

function EditContent(onlineContentId) {
    $.ajax({
        type: "POST",
        url: window.MAIN_WEB_PATH + "Ajax/GetOnlineContent",
        dataType: "json",
        data: { onlineContentId: onlineContentId },
        success: function (data, textStatus, jqXHR) {
            window.OnlineContent = JSON.parse(data);
            if (typeof loadStepData == "function") {
                loadStepData();
            }
            $('#add-online-content').dialog('open');
        }
    });
    return false;
}

function DeleteContent(onlineContentId) {
    $.ajax({
        type: "POST",
        url: window.MAIN_WEB_PATH + "Ajax/DeleteContent",
        dataType: "json",
        data: { onlineContentId: onlineContentId },
        success: function (data, textStatus, jqXHR) {
            window.location.reload(true);
        }
    });
}

function MarkOnlineContent(onlineContentId, sender) {
    $.ajax({
        type: "POST",
        url: window.MAIN_WEB_PATH + "Ajax/MarkOnlineContent",
        dataType: "json",
        data: { onlineContentId: onlineContentId, marked: $(sender)[0].checked },
        success: function (data, textStatus, jqXHR) {
        }
    });
}
function ClaimOnlineContent(onlineContentId, userId) {
    $.ajax({
        type: "POST",
        url: window.MAIN_WEB_PATH + "Ajax/AddClaim",
        dataType: "json",
        data: { onlineContentId: onlineContentId, userId: userId },
        success: function (data, textStatus, jqXHR) {
            $("#content-" + onlineContentId + " .ClaimValue").text(data);
        },
        error: function (data, textStatus, jqXHR) { return; }
    });
}